package com.example.day9;

import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    Spinner spinner;
    List<Map<String,Object>> list;
    String[] week;
    ImageView image;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spinner=findViewById(R.id.spinner);

        spinner.setPrompt("选择日期");//设置标题文字
//        setArraySpinner();
        setSimpleSpinner();
        spinner.setSelection(4);//设置默认选项
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//                image.setImageResource((int)list.get(position).get("image"));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    public void setArraySpinner() {
        week = getResources().getStringArray(R.array.selectDate_week);
        ArrayAdapter<String> arrAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,week);//默认布局
        arrAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//同样是默认布局
        spinner.setAdapter(arrAdapter);
    }

    public void setSimpleSpinner(){
        String[] week = getResources().getStringArray(R.array.selectDate_week);
        list=new ArrayList<Map<String,Object>>();
        for(String a:week){
            Map<String,Object> map = new HashMap<>();
            if(a.equals("周六")||a.equals("周日")){
                map.put("image",R.mipmap.happy);
                map.put("text",a);
            }else {
                map.put("image", R.mipmap.unhappy);
                map.put("text", a);
            }
            list.add(map);
        }
        SimpleAdapter adapter =
                new SimpleAdapter(this ,
                        list ,
                        R.layout.simple_item ,
                        new String[]{"text","image"},
                        new int[]{ R.id.text_item,R.id.image_item });
        spinner.setAdapter(adapter);
    }
}
